﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace TMS
{
    public partial class ProjectsControl : UserControl
    {
        private bool listOfProjectsLoaded = false;
        Users ActiveUser = Program.GetActiveUser;
        
        Project project = new Project();
        DataTable table = new DataTable();


        public ProjectsControl()
        {
            Program.GetMainForm.Text = "Проекти";
            InitializeComponent();
            if (!listOfProjectsLoaded)
            {
                loadListOfProjects();
                listOfProjectsLoaded = true;
            }
            ToStcmbSearch.SelectedItem = "Само мои";


        }

        private void loadListOfProjects()
        {
            table = project.GetAllProjectsFromDB().Tables[0];
            table.DefaultView.RowFilter = string.Format("Responsible ='" + ActiveUser.GetName() + " " + ActiveUser.GetSurname() + "'");
            dgvProjectList.DataSource = table;
            dgvProjectList.Columns[0].Visible = false;
            dgvProjectList.Columns[1].HeaderText = "Име";
            dgvProjectList.Columns[2].HeaderText = "Започнува";
            dgvProjectList.Columns[3].HeaderText = "Завршува";
            dgvProjectList.Columns[4].HeaderText = "Статус";
            dgvProjectList.Columns[5].HeaderText = "Комитент";
            dgvProjectList.Columns[6].HeaderText = "Одговорен";
        }


        private void dgvProjectsList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (ActiveUser.EditProjects == true)
            {
                DataGridViewRow row = dgvProjectList.CurrentRow;
                Program.GetMainForm.registerInHistoryMain(ControlsEnum.CREATE_EDIT_PROJECT_CONTROL, row);
                Program.GetMainForm.ShowControl(ControlsEnum.CREATE_EDIT_PROJECT_CONTROL, row);
                this.Dispose();
            }
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            if (ActiveUser.EditProjects == true)
            {
                this.Hide();
                Program.GetMainForm.registerInHistoryMain(ControlsEnum.CREATE_EDIT_PROJECT_CONTROL,null);
                Program.GetMainForm.ShowControl(ControlsEnum.CREATE_EDIT_PROJECT_CONTROL);
            }
            else
                MessageBox.Show("Немате доволно привилегии за додавање на Проект");
        }

        private void toolStripComboBox1_Click(object sender, EventArgs e)
        {
            try
            {
                if (ToStcmbSearch.SelectedItem.ToString() == "Сите")
                {
                    table.DefaultView.RowFilter = string.Format("");
                    dgvProjectList.Update();
                }
                else
                {
                    table.DefaultView.RowFilter = string.Format("Responsible ='" + ActiveUser.GetName() + " " + ActiveUser.GetSurname() + "'");
                    dgvProjectList.Update();
                }
            }
            catch
            {
            }
        }


        private void ToStBack_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Back())
                this.Dispose();
        }


        private void ToStForward_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Forward())
                this.Dispose();
        }

    }
}

